<template>
    <Card :bordered="false" dis-hover class="ivu-mt">
        <Row>
            <Col :xl="{span: 12, offset: 6}" :lg="{span: 12, offset: 6}" :md="{span: 16, offset: 4}"
                 :sm="{span: 20, offset: 2}" :xs="24">
                <Form ref="form" :model="data" :rules="rules" class="ivu-mt" :label-width="labelWidth"
                      :label-position="labelPosition">
                    <FormItem label="用车日期：" prop="startTime" label-for="startTime">
                        <Date-picker v-model="data.startTime"
                                     format="yyyy-MM-dd HH:mm:ss"
                                     type="datetime" placeholder="用车日期" style="width: 200px"></Date-picker>
                    </FormItem>
                    <FormItem label="返回日期：" prop="endTime" label-for="endTime">
                        <Date-picker
                                     v-model="data.endTime"
                                     format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="返回日期"
                                     style="width: 200px"></Date-picker>
                    </FormItem>
                    <FormItem label="用车出发地点：" prop="carAddress" label-for="carAddress">
                        <Input v-model="data.carAddress" placeholder="用车出发地点（必填）" element-id="carAddress"/>
                    </FormItem>

                    <FormItem label="用车数量：" prop="adtrtCount" label-for="adtrtCount">
                        <Input v-model="data.adtrtCount" placeholder="请输入数量（必填）" element-id="adtrtCount"/>
                    </FormItem>
                    <FormItem label="用车事由：" prop="adtrtReson" label-for="adtrtReson">
                        <Input type="textarea" :rows="4" v-model="data.adtrtReson" placeholder="描述清楚（需要车乘坐的人数等信息）"
                               element-id="adtrtReson"/>
                    </FormItem>
                    <FormItem>
                        <Button class="ivu-ml" @click="handleReset(data)">取消</Button>
                        <Button class="ivu-ml" type="primary" :loading="loading" @click="handleSubmit">确定</Button>
                    </FormItem>

                </Form>
            </Col>
        </Row>
    </Card>

</template>

<script>
    import Administrative from "@/api/Administrative";

    export default {
        data() {
            return {
                trafficType: '',
                formItem: {
                    checkbox: [],
                },
                data: {
                    startTime: '',
                    endTime: '',
                    carAddress: '',
                    adtrtCount: '',
                    adtrtReson: '',
                    birthday: '',
                },
                loading: false,
                rules: {
                    startTime: [
                        {required: true, type: 'date', message: '用车日期（必填', trigger: 'change'}
                    ],
                    endTime: [
                        {required: true, type: 'date', message: '返回日期（必填', trigger: 'change'}
                    ],
                    carAddress: [
                        {required: true, message: '用车出发地点', trigger: 'blur'}
                    ],
                    adtrtCount: [
                        {required: true, message: '请输入数量', trigger: 'blur'}
                    ],
                    adtrtReson: [
                        {required: true, message: '描述清楚（需要车乘坐的人数等信息', trigger: 'blur'}
                    ],
                },
            }
        },
        computed: {
            labelWidth() {
                return this.isMobile ? undefined : 140;
            },
            labelPosition() {
                return this.isMobile ? 'top' : 'left';
            }
        },
        created() {
            let baseData = this.$route.params.item;
            let myIndex = this.$route.params.myIndex;
            console.log(myIndex);
            //是否编辑渲染
            if (myIndex) {
                this.data.contractId = baseData.positionName;
                this.data.mechanismOwner = baseData.mechanismOwner;

            }
        },
        methods: {


// T和Z时间转换
            changeDate(oldDate) {
                var dateee = new Date(oldDate).toJSON();//这个ie不兼容，会返回1970年的时间
                var date = new Date(+new Date(dateee) + 8 * 3600 * 1000)
                    .toISOString()
                    .replace(/T/g, " ")
                    .replace(/\.[\d]{3}Z/, "");
                return date;
            },

            //新增
            handleSubmit() {
                let newData = this.changeDate(this.data.startTime)
                console.log(newData);
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.loading = true;
                        Administrative.insertCarOrZang({
                            adtrtType: 2, //行政类型（1：物品领用，2：用车申请，3：永章申请）
                            // status: 1 //审核状态（1：通过，2：未审核
                            startTime: this.changeDate(this.data.startTime),
                            endTime: this.changeDate(this.data.endTime),
                            carAddress: this.data.carAddress,
                            adtrtCount: this.data.adtrtCount,
                            adtrtReson: this.data.adtrtReson,
                            userId:this.$store.state.admin.user.info.userId,

                        })
                            .then(res => {
                                // 返回数据
                                this.loading = false;
                                this.$Message.success('新增成功');
                                this.$router.push('/administrative/car')
                            })
                            .catch(err => {
                                // 异常情况

                            })
                    } else {
                        this.loading = false;
                    }
                });
            },
            // 新增数据
            handleCreate() {
                this.$refs.create.validate((valid) => {
                    if (valid) {
                        if (this.updateIndex < 0) {
                            // 新建
                            const mockData = this.mockOneData();
                            mockData.desc = this.createData.desc;
                            mockData.date = new Date();
                            this.list.splice(0, 0, mockData);
                            this.$Message.success('创建成功');
                        } else {
                            // 修改
                            const updateItemName = this.dataWithPage[this.updateIndex].name;
                            const updateItemIndex = this.list.findIndex(item => item.name === updateItemName);
                            this.list[updateItemIndex].desc = this.createData.desc;
                            this.$Message.success('修改成功');
                        }

                        this.showCreate = false;
                        this.creating = false;
                        this.$nextTick(() => {
                            this.creating = true;
                        });
                    } else {
                        this.creating = false;
                        this.$nextTick(() => {
                            this.creating = true;
                        });
                    }
                });
            },
            //取消
            handleReset(name) {
                // this.$refs[name].resetFields();
                this.$router.push('/administrative/car')
            },

        }

    }
</script>

<style scoped>

</style>
